package com.umeox.watch.audiobooks.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

/**
 * Created by Rambo 2024/5/16/0016
 * Describe:
 */
public class StoryProgressView extends View {
    private static final float START_ANGLE = 15f;
    private static final float MAX_ANGLE = 150f;
    private float progress = 0;
    private float max = 100;
    private float centerX;
    private float centerY;
    private float width;
    private float height;
    private int lineWidth = dp2px(5); // 绘制圆环的线宽
    private int pointWidth = dp2px(2); // 绘制圆点线宽
    private int rectangleWidth = dp2px(1); // 绘制矩形的线宽

    private SweepGradient sweepGradient;

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint rectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private ObjectAnimator animator;

    private RectF mRectF;

    public StoryProgressView(Context context) {
        this(context, null);
    }

    public StoryProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StoryProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint.setColor(Color.parseColor("#2EC26C"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(lineWidth);

        bgPaint.setColor(Color.parseColor("#4C2EC26C"));
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeCap(Paint.Cap.ROUND);
        bgPaint.setStrokeWidth(lineWidth);

        pointPaint.setColor(Color.WHITE);
        pointPaint.setStyle(Paint.Style.STROKE);
        pointPaint.setStrokeWidth(pointWidth);

        rectPaint.setColor(Color.WHITE);

        mRectF = new RectF();
    }


    public void setMax(float max) {
        this.max = max;
    }

    public void setProgress(float progress) {
        this.progress = progress;
        invalidate();
    }

    public float getProgress(){
        return progress;
    }

    public void setProgressWithAnim(float progress) {
        animator = ObjectAnimator.ofFloat(this, "progress", 0, progress);
        animator.setDuration(2000);
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getWidth();
        height = getHeight();
        centerX = (float) getWidth() / 2;
        centerY = (float) getHeight() / 2;
//        sweepGradient = new SweepGradient(centerX, centerY, colors, null);
//        //改变开始渐变的角度(默认从右边开始画圆渐变,旋转90度就从下方开始画圆渐变,旋转之后更好设置颜色渐变值)
//        Matrix matrix = new Matrix();
//        matrix.setRotate(90, centerX, centerY);
//        sweepGradient.setLocalMatrix(matrix);
//        paint.setShader(sweepGradient);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制环形
        drawArc(canvas);
        // 绘制矩形白条
//        draRectangle(canvas);
        // 绘制末尾圆点
//        drawPoint(canvas);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawArc(Canvas canvas) {
        float sweepAngle = MAX_ANGLE * (progress / max);
        int padding = Math.round(lineWidth / 2f);
        mRectF.set(padding, padding, width - padding, height - padding);
        canvas.drawArc(mRectF, START_ANGLE, MAX_ANGLE, false, bgPaint);
        canvas.drawArc(mRectF, MAX_ANGLE + START_ANGLE, -sweepAngle, false, paint);
    }

    private void draRectangle(Canvas canvas) {
        float sweepAngle = MAX_ANGLE * progress / 100;
        if (sweepAngle > 70) {
            // 旋转到左侧
            canvas.save();
            canvas.rotate(-80f, centerX, centerY);
            canvas.drawRect(centerX - rectangleWidth, rectangleWidth, centerX + rectangleWidth,dp2px(20), rectPaint);
            canvas.restore();
        }
        if (sweepAngle > 150f) {
            //绘制中间矩形
            canvas.drawRect(centerX - rectangleWidth, 0, centerX + rectangleWidth, dp2px(20), rectPaint);
        }
        if (sweepAngle > 230f) {
            // 旋转到右侧
            canvas.save();
            canvas.rotate(80f, centerX, centerY);
            canvas.drawRect(centerX - rectangleWidth, 0, centerX + rectangleWidth, dp2px(20), rectPaint);
            canvas.restore();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void drawPoint(Canvas canvas) {
        Path path = new Path();
        float sweepAngle = MAX_ANGLE * progress / 100;
        int padding = lineWidth + pointWidth;
        path.addArc(padding, padding, width - padding, height - padding, START_ANGLE, sweepAngle);
        PathMeasure measure = new PathMeasure(path, false);
        float[] pos = new float[2];
        measure.getPosTan(measure.getLength() - 1, pos, null);
        pointPaint.setColor(Color.parseColor("#7F43FB"));
        pointPaint.setStyle(Paint.Style.FILL);//仅填充内部
        canvas.drawCircle(pos[0], pos[1], lineWidth - pointWidth, pointPaint);

        pointPaint.setColor(Color.WHITE);
        pointPaint.setStyle(Paint.Style.STROKE);//仅描边
        canvas.drawCircle(pos[0], pos[1], lineWidth - pointWidth, pointPaint);
    }

    public int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
